﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using Meta.Numerics.Statistics;

namespace StatisticsApplication.MyClass
{
    class CombinationClass
    {
        public BackgroundWorker backgroundWorker1;
        public CombinationClass(BackgroundWorker temp)
        {
            backgroundWorker1 = temp;
        }
        public string Twosort(string[] numbers, int se1, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;

            // Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            string[] ww = t1.Remain;
            int s1count = -1;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                //singles.Enqueue(s1 + "| " + t1.Remain[++s1count]);
                //txbox.AppendText(s1 + "| " + t1.Remain[++s1count] + "\r\n");

                if (backgroundWorker1.CancellationPending)
                {
                    result = "cancle";
                    return result;
                }
                backgroundWorker1.ReportProgress(howmany);
                ++howmany;
                string str = s1 + "| " + t1.Remain[++s1count] + "\r\n";

                double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                double[] a2 = MyConvertToNum(t1.Remain[s1count], new char[] { ' ' });

                Sample group1 = new Sample(a1);
                Sample group2 = new Sample(a2);

                OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2);
                if (howmany==1)
                {
                    originalvalue = oresult.FTest.Statistic;
                }
                else if (originalvalue < oresult.FTest.Statistic)
                {
                    larger++;                    
                }
                //Console.WriteLine(result.FTest.RightProbability);
                str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();

                // AppendString(str, txbox);

            }
            result = "ok"+larger.ToString();
            return result;
        }

        public string Threesort(string[] numbers, int se1, int se2, ListBox txbox)
        {
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string result;
            int larger = 0;
            double originalvalue = 0;
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            string[] ww = t1.Remain;
            int s1count = -1;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = ww[++s1count].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);
                int t2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "| " + s2 + "| " + t2.Remain[t2count++]);
                    // txbox.AppendText(s1 + "| " + s2 + "| " + t2.Remain[t2count++] + "\r\n");
                    if (backgroundWorker1.CancellationPending)
                    {
                        result = "cancle";
                        return result;
                    }
                    backgroundWorker1.ReportProgress(howmany);
                    ++howmany;
                    string str = s1 + "| " + s2 + "| " + t2.Remain[t2count];

                    //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                    double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                    double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                    double[] a3 = MyConvertToNum(t2.Remain[t2count], new char[] { ' ' });

                    Sample group1 = new Sample(a1);
                    Sample group2 = new Sample(a2);
                    Sample group3 = new Sample(a3);

                    OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3);
                    //Console.WriteLine(result.FTest.RightProbability);
                    if (howmany == 1)
                    {
                        originalvalue = oresult.FTest.Statistic;
                    }
                    else if (originalvalue < oresult.FTest.Statistic)
                    {
                        larger++;
                    }
                    str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                    t2count++;
                    //AppendString(str, txbox);
                }
            }
            result = "ok" + larger.ToString();
            return result;
        }

        public string Foursort(string[] numbers, int se1, int se2, int se3, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            // Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);

                        //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++] + "\r\n");
                        if (backgroundWorker1.CancellationPending)
                        {
                            result = "cancle";
                            return result;
                        }
                      backgroundWorker1.ReportProgress(howmany);
                        ++howmany;

                        string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                        double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                        double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                        double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                        double[] a4 = MyConvertToNum(t3.Remain[s3count], new char[] { ' ' });

                        Sample group1 = new Sample(a1);
                        Sample group2 = new Sample(a2);
                        Sample group3 = new Sample(a3);
                        Sample group4 = new Sample(a4);

                        OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4);
                        //Console.WriteLine(result.FTest.RightProbability);
                        str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                        if (howmany == 1)
                        {
                            originalvalue = oresult.FTest.Statistic;
                        }
                        else if (originalvalue < oresult.FTest.Statistic)
                        {
                            larger++;
                        }
                        // AppendString(str, txbox);
                        s3count++;
                    }
                }
            }
            result = "ok" + larger.ToString();
            return result;
        }

        public string Fivesort(string[] numbers, int se1, int se2, int se3, int se4, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);
                            if (backgroundWorker1.CancellationPending)
                            {
                                result = "cancle";
                                return result;
                            }
                         //backgroundWorker1.ReportProgress(howmany);
                            ++howmany;
                            string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count];

                            //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                            double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                            double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                            double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                            double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                            double[] a5 = MyConvertToNum(t4.Remain[s4count], new char[] { ' ' });

                            Sample group1 = new Sample(a1);
                            Sample group2 = new Sample(a2);
                            Sample group3 = new Sample(a3);
                            Sample group4 = new Sample(a4);
                            Sample group5 = new Sample(a5);

                            OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5);
                            //Console.WriteLine(result.FTest.RightProbability);
                            str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                            if (howmany == 1)
                            {
                                originalvalue = oresult.FTest.Statistic;
                            }
                            else if (originalvalue < oresult.FTest.Statistic)
                            {
                                larger++;
                            }
                            //AppendString(str, txbox);
                            s4count++;
                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }



        public string Sixsort(string[] numbers, int se1, int se2, int se3, int se4, int se5, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);

                            string[] ee = t4.Remain[s4count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            MyElementClass t5 = getPortfolio(ee, se5);
                            int s5count = 0;

                            foreach (string s5 in t5.Select)
                            {
                                if (backgroundWorker1.CancellationPending)
                                {
                                    result = "cancle";
                                    return result;
                                }
                                backgroundWorker1.ReportProgress(howmany);
                                ++howmany;
                                string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + s5 + "|" + t5.Remain[s5count];

                                //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                                double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                                double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                                double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                                double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                                double[] a5 = MyConvertToNum(s5, new char[] { ' ' });
                                double[] a6 = MyConvertToNum(t5.Remain[s5count], new char[] { ' ' });

                                Sample group1 = new Sample(a1);
                                Sample group2 = new Sample(a2);
                                Sample group3 = new Sample(a3);
                                Sample group4 = new Sample(a4);
                                Sample group5 = new Sample(a5);
                                Sample group6 = new Sample(a6);

                                OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5, group6);
                                //Console.WriteLine(result.FTest.RightProbability);
                                str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                                if (howmany == 1)
                                {
                                    originalvalue = oresult.FTest.Statistic;
                                    
                                }
                                else if (originalvalue < oresult.FTest.Statistic)
                                {
                                    larger++;
                                }
                                //AppendString(str, txbox);
                                s5count++;
                            }



                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }

        public string Sevensort(string[] numbers, int se1, int se2, int se3, int se4, int se5, int se6, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);

                            string[] ee = t4.Remain[s4count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            MyElementClass t5 = getPortfolio(ee, se5);
                            int s5count = 0;

                            foreach (string s5 in t5.Select)
                            {


                                string[] ff = t5.Remain[s5count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                MyElementClass t6 = getPortfolio(ff, se6);
                                int s6count = 0;
                                foreach (string s6 in t6.Select)
                                {
                                    if (backgroundWorker1.CancellationPending)
                                    {
                                        result = "cancle";
                                        return result;
                                    }
                                    backgroundWorker1.ReportProgress(howmany);
                                    ++howmany;
                                    string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + s5 + "|" + s6 + "|" + t6.Remain[s6count];

                                    //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                                    double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                                    double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                                    double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                                    double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                                    double[] a5 = MyConvertToNum(s5, new char[] { ' ' });
                                    double[] a6 = MyConvertToNum(s6, new char[] { ' ' });
                                    double[] a7 = MyConvertToNum(t6.Remain[s6count], new char[] { ' ' });

                                    Sample group1 = new Sample(a1);
                                    Sample group2 = new Sample(a2);
                                    Sample group3 = new Sample(a3);
                                    Sample group4 = new Sample(a4);
                                    Sample group5 = new Sample(a5);
                                    Sample group6 = new Sample(a6);
                                    Sample group7 = new Sample(a7);

                                    OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5, group6, group7);
                                    //Console.WriteLine(result.FTest.RightProbability);
                                    str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                                    if (howmany == 1)
                                    {
                                        originalvalue = oresult.FTest.Statistic;
                                    }
                                    else if (originalvalue < oresult.FTest.Statistic)
                                    {
                                        larger++;
                                    }
                                    //AppendString(str, txbox);
                                    s6count++;
                                }

                            }

                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }
        public string Eightsort(string[] numbers, int se1, int se2, int se3, int se4, int se5, int se6, int se7, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);

                            string[] ee = t4.Remain[s4count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            MyElementClass t5 = getPortfolio(ee, se5);
                            int s5count = 0;

                            foreach (string s5 in t5.Select)
                            {


                                string[] ff = t5.Remain[s5count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                MyElementClass t6 = getPortfolio(ff, se6);
                                int s6count = 0;
                                foreach (string s6 in t6.Select)
                                {
                                    string[] gg = t6.Remain[s6count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    MyElementClass t7 = getPortfolio(gg, se7);
                                    int s7count = 0;
                                    foreach (string s7 in t7.Select)
                                    {


                                        if (backgroundWorker1.CancellationPending)
                                        {
                                            result = "cancle";
                                            return result;
                                        }
                                        backgroundWorker1.ReportProgress(howmany);
                                        ++howmany;
                                        string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + s5 + "|" + s6 + "|" + s7 + "|" + t7.Remain[s7count];

                                        //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                                        double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                                        double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                                        double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                                        double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                                        double[] a5 = MyConvertToNum(s5, new char[] { ' ' });
                                        double[] a6 = MyConvertToNum(s6, new char[] { ' ' });
                                        double[] a7 = MyConvertToNum(s7, new char[] { ' ' });
                                        double[] a8 = MyConvertToNum(t7.Remain[s7count], new char[] { ' ' });

                                        Sample group1 = new Sample(a1);
                                        Sample group2 = new Sample(a2);
                                        Sample group3 = new Sample(a3);
                                        Sample group4 = new Sample(a4);
                                        Sample group5 = new Sample(a5);
                                        Sample group6 = new Sample(a6);
                                        Sample group7 = new Sample(a7);
                                        Sample group8 = new Sample(a8);

                                        OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5, group6, group7, group8);
                                        //Console.WriteLine(result.FTest.RightProbability);
                                        str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                                        if (howmany == 1)
                                        {
                                            originalvalue = oresult.FTest.Statistic;
                                        }
                                        else if (originalvalue < oresult.FTest.Statistic)
                                        {
                                            larger++;
                                        }
                                        //AppendString(str, txbox);
                                        s7count++;

                                    }
                                }

                            }

                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }
        public string Ninesort(string[] numbers, int se1, int se2, int se3, int se4, int se5, int se6, int se7,int se8 ,ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);

                            string[] ee = t4.Remain[s4count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            MyElementClass t5 = getPortfolio(ee, se5);
                            int s5count = 0;

                            foreach (string s5 in t5.Select)
                            {


                                string[] ff = t5.Remain[s5count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                MyElementClass t6 = getPortfolio(ff, se6);
                                int s6count = 0;
                                foreach (string s6 in t6.Select)
                                {
                                    string[] gg = t6.Remain[s6count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    MyElementClass t7 = getPortfolio(gg, se7);
                                    int s7count = 0;
                                    foreach (string s7 in t7.Select)
                                    {

                                        string[] hh = t7.Remain[s7count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                        MyElementClass t8 = getPortfolio(hh,se8);
                                        int s8count = 0;

                                        foreach(string s8 in t8.Select)
                                        {

                                            if (backgroundWorker1.CancellationPending)
                                            {
                                                result = "cancle";
                                                return result;
                                            }
                                            backgroundWorker1.ReportProgress(howmany);
                                            ++howmany;
                                            string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + s5 + "|" + s6 + "|" + s7 + "|" + s8 + "|" + t8.Remain[s8count];

                                            //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                                            double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                                            double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                                            double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                                            double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                                            double[] a5 = MyConvertToNum(s5, new char[] { ' ' });
                                            double[] a6 = MyConvertToNum(s6, new char[] { ' ' });
                                            double[] a7 = MyConvertToNum(s7, new char[] { ' ' });
                                            double[] a8 = MyConvertToNum(s8, new char[] { ' ' });
                                            double[] a9 = MyConvertToNum(t8.Remain[s8count], new char[] { ' ' });

                                            Sample group1 = new Sample(a1);
                                            Sample group2 = new Sample(a2);
                                            Sample group3 = new Sample(a3);
                                            Sample group4 = new Sample(a4);
                                            Sample group5 = new Sample(a5);
                                            Sample group6 = new Sample(a6);
                                            Sample group7 = new Sample(a7);
                                            Sample group8 = new Sample(a8);
                                            Sample group9 = new Sample(a9);

                                            OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5, group6, group7, group8,group9);
                                            //Console.WriteLine(result.FTest.RightProbability);
                                            str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                                            if (howmany == 1)
                                            {
                                                originalvalue = oresult.FTest.Statistic;
                                            }
                                            else if (originalvalue < oresult.FTest.Statistic)
                                            {
                                                larger++;
                                            }
                                            //AppendString(str, txbox);
                                            s8count++;

                                        }

                                      

                                    }
                                }

                            }

                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }
        public string Tensort(string[] numbers, int se1, int se2, int se3, int se4, int se5, int se6, int se7, int se8,int se9, ListBox txbox)
        {
            string result;
            int larger = 0;
            double originalvalue = 0;
            //Queue<string> singles = new Queue<string>(); //重组生成的字符串的集合
            string[] kk = numbers; //Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//先分组
            MyElementClass t1 = getPortfolio(kk, se1);//从kk分组中去取出S1个数，的组合方法。
            // string[] ww = t1.Remain;
            int s1count = 0;
            int howmany = 0;
            foreach (string s1 in t1.Select)
            {
                // s1count++;
                string[] bb = t1.Remain[s1count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                MyElementClass t2 = getPortfolio(bb, se2);


                // string[] xx = t2.Remain;
                //int s2count=-1;                
                int s2count = 0;
                foreach (string s2 in t2.Select)
                {
                    // Console.WriteLine(s1 + s2 + t2.Remain[t2count++]);  
                    //singles.Enqueue(s1 + "|" + s2 + "|" + t2.Remain[t2count++]);
                    string[] cc = t2.Remain[s2count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    MyElementClass t3 = getPortfolio(cc, se3);
                    int s3count = 0;
                    foreach (string s3 in t3.Select)
                    {
                        string[] dd = t3.Remain[s3count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        MyElementClass t4 = getPortfolio(dd, se4);
                        int s4count = 0;
                        foreach (string s4 in t4.Select)
                        {
                            //txbox.AppendText(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++] + "\r\n");
                            //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + t4.Remain[s4count++]);

                            string[] ee = t4.Remain[s4count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            MyElementClass t5 = getPortfolio(ee, se5);
                            int s5count = 0;

                            foreach (string s5 in t5.Select)
                            {


                                string[] ff = t5.Remain[s5count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                MyElementClass t6 = getPortfolio(ff, se6);
                                int s6count = 0;
                                foreach (string s6 in t6.Select)
                                {
                                    string[] gg = t6.Remain[s6count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    MyElementClass t7 = getPortfolio(gg, se7);
                                    int s7count = 0;
                                    foreach (string s7 in t7.Select)
                                    {

                                        string[] hh = t7.Remain[s7count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                        MyElementClass t8 = getPortfolio(hh, se8);
                                        int s8count = 0;

                                        foreach (string s8 in t8.Select)
                                        {


                                            string[] ii = t8.Remain[s8count++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                            MyElementClass t9 = getPortfolio(ii, se9);
                                            int s9count = 0;

                                            foreach(string s9 in t9.Select)
                                            {
                                                if (backgroundWorker1.CancellationPending)
                                                {
                                                    result = "cancle";
                                                    return result;
                                                }

                                                backgroundWorker1.ReportProgress(howmany);
                                                ++howmany;
                                                string str = s1 + "| " + s2 + "| " + s3 + "| " + s4 + "| " + s5 + "|" + s6 + "|" + s7 + "|" + s8 + "|" + s9 + "|" + t9.Remain[s9count];

                                                //string str = s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count];
                                                double[] a1 = MyConvertToNum(s1, new char[] { ' ' });
                                                double[] a2 = MyConvertToNum(s2, new char[] { ' ' });
                                                double[] a3 = MyConvertToNum(s3, new char[] { ' ' });
                                                double[] a4 = MyConvertToNum(s4, new char[] { ' ' });
                                                double[] a5 = MyConvertToNum(s5, new char[] { ' ' });
                                                double[] a6 = MyConvertToNum(s6, new char[] { ' ' });
                                                double[] a7 = MyConvertToNum(s7, new char[] { ' ' });
                                                double[] a8 = MyConvertToNum(s8, new char[] { ' ' });
                                                double[] a9 = MyConvertToNum(s9, new char[] { ' ' });
                                                double[] a10 = MyConvertToNum(t9.Remain[s9count], new char[] { ' ' });

                                                Sample group1 = new Sample(a1);
                                                Sample group2 = new Sample(a2);
                                                Sample group3 = new Sample(a3);
                                                Sample group4 = new Sample(a4);
                                                Sample group5 = new Sample(a5);
                                                Sample group6 = new Sample(a6);
                                                Sample group7 = new Sample(a7);
                                                Sample group8 = new Sample(a8);
                                                Sample group9 = new Sample(a9);
                                                Sample group10 = new Sample(a10);

                                                OneWayAnovaResult oresult = Sample.OneWayAnovaTest(group1, group2, group3, group4, group5, group6, group7, group8, group9,group10);
                                                //Console.WriteLine(result.FTest.RightProbability);
                                                str += "|result: Ftest=" + oresult.FTest.Statistic.ToString();
                                                //AppendString(str, txbox);
                                                if (howmany == 1)
                                                {
                                                    originalvalue = oresult.FTest.Statistic;
                                                }
                                                else if (originalvalue < oresult.FTest.Statistic)
                                                {
                                                    larger++;
                                                }
                                                s9count++;
                                            }                                   

                                        }
                                    }
                                }
                            }
                        }
                        //singles.Enqueue(s1 + "| " + s2 + "| " + s3 + "| " + t3.Remain[s3count++]);
                    }
                }
            }
            //return singles.ToArray();
            result = "ok" + larger.ToString();
            return result;
        }


        delegate void AppendStringDelegate(string str, ListBox tx);
        private void AppendString(string str, ListBox tx)
        {
            if (tx.InvokeRequired)
            {
                AppendStringDelegate dd = new AppendStringDelegate(AppendString);
                //tx.Invoke(str, tx);
                object[] temp = { str, tx };
                tx.Invoke(dd, temp);
            }
            else
            {
                tx.Items.Insert(0, str);
            }
        }




        /// <summary>
        /// 
        /// </summary>
        /// <param name="numbers">从哪个数组里面取</param>
        /// <param name="n">取多少个数字</param>
        /// <returns>返回组合数列。两部分一部分是取的，另一部分是剩余的。</returns>
        public MyElementClass getPortfolio(String[] numbers, int n)
        {
            int m = numbers.Length;  //参数数组长度
            int l = 1;               //构造数组长度标志

            for (int j = m; j > m - n; j--) //获得排列数组长度L
            {
                l = l * j;
            }

            for (int z = n; z > 1; z--)
            {
                l = l / z;
            }

            String[] rs = new String[l];           //返回结果
            String[] remain = new String[l];
            String str = "";                     //将第一种排列存入数组RS（即所有1都在左边的情况）
            for (int x = 0; x < n; x++)
            {
                str = str + numbers[x] + " ";
            }
            rs[0] = str;

            str = "";
            for (int x = n; x < m; x++)
            {
                str = str + numbers[x] + " ";
            }

            remain[0] = str;
            int[] s = new int[m];    //构造下标数组
            bool flag = true;     //循环开关
            int k = 1;               //返回结果数组长度（自增长）

            for (int i = 0; i < m; i++)   //初始化构造下标数组
            {
                if (i < n)
                    s[i] = 1;
                else
                    s[i] = 0;
            }
            do
            {
                flag = false;                        //初始FLAG
                int zerocount = 0;                  //10转换01前的0的个数
                for (int i = 0; i < m - 1; i++)
                {
                    if (s[i] == 0)                   //记录前0个数（非0即1），可以通过这个参数进行1的前移 
                    {
                        zerocount++;
                    }
                    if (s[i] == 1 && s[i + 1] == 0)        //10变成01 
                    {
                        s[i] = 0; s[i + 1] = 1;

                        flag = true;                 //如果成功转换，flag设置为0，如果没有证明所以1已经移动到最后，故可以跳出DO循环

                        for (int j = 0; j < i; j++)       //10转换01前的所有1前移操作
                        {
                            if (j < i - zerocount)
                                s[j] = 1;
                            else
                                s[j] = 0;
                        }
                        String returnstr = "";       //用于存储变化后的构造数组
                        String remainstr = "";
                        for (int kk = 0; kk < m; kk++)    //通过构造数组下标，得到需要的返回串
                        {
                            if (s[kk] == 1)
                            {
                                returnstr = returnstr + numbers[kk] + " ";
                            }
                            if (s[kk] == 0)
                            {
                                remainstr = remainstr + numbers[kk] + " ";

                            }
                        }
                        rs[k] = returnstr;          //将串存入RS数组，用于返回   
                        remain[k] = remainstr;
                        i = m;                       //转换了第一个10后，就要跳出该次FOR循环，所以将i直接置成m
                        k++;                       //rs返回数组下标向后推一位，用于存储下个返回串
                    }
                }
            } while (flag == true);

            MyElementClass myelement = new MyElementClass(rs, remain);
            return myelement;
        }


        private double[] MyConvertToNum(string example, char[] splitflag)
        {
            //new char[] { ' ' }
            string[] temp = example.Split(splitflag, StringSplitOptions.RemoveEmptyEntries);
            int count = temp.Length;
            double[] myresult = new double[count];
            for (int i = 0; i < count; i++)
            {
                myresult[i] = Convert.ToDouble(temp[i]);
            }
            return myresult;
        }
    }
}
